import sys
import os
import re
import ConfigParser
import globalvar
from optparse import OptionParser


def gen_kernel_diff(oldfile,newfile):
    
    config = ConfigParser.ConfigParser()
    config.read('products.ini')
    print 'the Model is %s'%(globalvar.MODEL)
    raw_cmd=config.get(globalvar.MODEL,'raw_cmd')
    
    up_cmd = "%s %s-%s_raw_upgrade.bin %s %s "%(raw_cmd,globalvar.VENDOR,globalvar.MODEL,oldfile,newfile)
    fd = os.popen(up_cmd)    
    if globalvar.dmlogging:
        globalvar.dmlogging.debug('[%s]'%(up_cmd))
    fd.close()
    

    up_cmd_debug = "vCurrentMobileCMD5.1.37.37.exe debug_trace %s-%s_raw_upgrade.bin %s-%s_raw_upgrade-debug.bin /setall=1"%(globalvar.VENDOR,globalvar.MODEL,globalvar.VENDOR,globalvar.MODEL)    
    fd = os.popen(up_cmd_debug)
    if globalvar.dmlogging:
        globalvar.dmlogging.debug('[%s]'%(up_cmd_debug))
    fd.close()    

        
        
    de_cmd = "%s %s-%s_raw_degrade.bin %s %s "%(raw_cmd,globalvar.VENDOR,globalvar.MODEL,newfile,oldfile)
    fd = os.popen(de_cmd)    
    if globalvar.dmlogging:
        globalvar.dmlogging.debug('[%s]'%(de_cmd))
    fd.close()    
    


    de_cmd_debug = "vCurrentMobileCMD5.1.37.37.exe  debug_trace %s-%s_raw_degrade.bin %s-%s_raw_degrade-debug.bin /setall=1"%(globalvar.VENDOR,globalvar.MODEL,globalvar.VENDOR,globalvar.MODEL)
    fd = os.popen(de_cmd_debug)    
    if globalvar.dmlogging:
        globalvar.dmlogging.debug('[%s]'%(de_cmd_debug))
    fd.close()   
    
    return 0
    

def main(*argv):
    """Generate the diff package using the """
    if argv:
        argv = list(argv)
    else:
        argv = sys.argv[1:]
        
    usage = "usage: fs_diff -i [infile]"    
    parser = OptionParser(usage=usage)    
    parser.add_option("-o", "--oldfile", dest="oldfile",type="string",help="old kernel ")
    parser.add_option("-n", "--newfile", dest="newfile",type="string",help="new kernel ")

    parser.add_option("-m", "--model", dest="model",type="string",help="model name of the phone ")
    
    (options, args) = parser.parse_args(args=argv)
 
    globalvar.MODEL = options.model
    oldfile = os.path.abspath(options.oldfile)
    newfile = os.path.abspath(options.newfile)
    
    ret = gen_kernel_diff(oldfile,newfile)
       
    print ret
        
if __name__ == "__main__":
    main()
    